/**
 * @author Francesco di Dio
 * Date: 20/apr/2013
 * Titolo: MainWindows.java
 * Versione: 
 */

/**
 * Copyright (c) 2013 Francesco di Dio.
 * tabuto83@gmail.com 
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tabuto.jkeys.views;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import org.apache.log4j.Logger;

import com.tabuto.jkeys.JKeysConstant;
import com.tabuto.jkeys.controllers.JKControllerInterface;

public class MainWindow extends JFrame implements JKViewInterface {
	
	private static final Logger log = Logger.getLogger(MainWindow.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -317805669230990847L;

	private JKControllerInterface jKeysController = null;
	private JKeysToolbar toolbar = null;
	private AccountsTreePanel treePanel = null;
	private AccountViewPanel accountViewPanel = null;
	private JKMenuBar jkmenubar = null;

	public MainWindow(JKControllerInterface jKeysController) {
		log.info("build MainWindow");
		Dimension d = new Dimension(JKeysConstant.WINDOW_WIDTH, JKeysConstant.WINDOW_HEIGHT);
		setSize(d.width, d.height);
		setPreferredSize(d);
		setResizable(true);

		// Setto il titolo della finestra principale
		setTitle(JKeysConstant.APPLICATION_NAME + " v." + JKeysConstant.APPLICATION_VERSION);

		// this.jKeysController = jKeysController;

		// Imposto il BorderLayout lasciando come margine tra i componenti
		// 5 pixel verticali e 5 pixel orizzontali
		setLayout(new BorderLayout(5, 5));

		setjKeysController(jKeysController);
		
	
		
		
		// initComponent();

		// Formatto i componenti grafici all'interno del frame per ottimizzarne
		// le dimensioni

		// Rendo visibile la finestra
		this.setVisible(true);
		// Chiudo la finesttra al click della 'X'
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

	private void initComponent() {
		toolbar = new JKeysToolbar(getjKeysController());

		jkmenubar = new JKMenuBar(getjKeysController());
		setJMenuBar(jkmenubar);

		treePanel = new AccountsTreePanel(getjKeysController());
		treePanel.setVisible(true);

		accountViewPanel = new AccountViewPanel(getjKeysController());
		accountViewPanel.setVisible(true);

		this.getContentPane().add(toolbar, BorderLayout.PAGE_START);
		this.getContentPane().add(treePanel, BorderLayout.LINE_START);
		this.getContentPane().add(accountViewPanel, BorderLayout.CENTER);

	}

	public JKControllerInterface getjKeysController() {
		return jKeysController;
	}

	public void setjKeysController(JKControllerInterface jKeysController) {
		this.jKeysController = jKeysController;
		// Inizializzo i componenti
		initComponent();
		pack();
	}



	private AccountViewPanel getAccountViewPanel() {
		return accountViewPanel;
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#getPasswordField()
	 */
	@Override
	public JPasswordField getPasswordField() {
		return getAccountViewPanel().getPassword();
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#getTitleField()
	 */
	@Override
	public JTextField getTitleField() {
		return getAccountViewPanel().getTitle();
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#getUsernameField()
	 */
	@Override
	public JTextField getUsernameField() {
		return getAccountViewPanel().getUsername();
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#getUrlField()
	 */
	@Override
	public JTextField getUrlField() {
		return getAccountViewPanel().getUrl();
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#getNotesField()
	 */
	@Override
	public JTextArea getNotesTextArea() {
		return getAccountViewPanel().getTxtrNotesArea();
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#getMainJFrame()
	 */
	@Override
	public JFrame getMainJFrame() {
		return this;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#refreshView()
	 */
	@Override
	public void refresh() {
		log.debug("Refresh view");
		refreshTree();
		refreshDetail();
		

	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#refreshTree()
	 */
	@Override
	public void refreshTree() {
		this.treePanel.refresh();
		
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#refreshDetail()
	 */
	@Override
	public void refreshDetail() {
		this.accountViewPanel.update();
		
	}

	/* (non-Javadoc)
	 * @see com.tabuto.jkeys.views.JKViewInterface#setShowPasswordMode(boolean)
	 */
	@Override
	public void switchShowPasswordMode() {
		this.accountViewPanel.switchShowPasswordMode();
		
	}


}
